<template>
      <div class="max_login">
  <div  class="main_login" style="width:600px;height:400px;margin:auto">
    <div style="margin:auto;width: max-content;">管理员登录</div>

    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top:70px">
        <el-form-item label="账号" prop="account">
            <el-input type="input" v-model="ruleForm.account" autocomplete="off" style="width:400px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off" style="width:400px"></el-input>
        </el-form-item>
         <el-form-item>
    <div style="width: max-content;margin: inherit;">
        <el-button type="primary" @click="submitForm('ruleForm')" style="margin-right: 40px;">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
    </div>
     </el-form-item>
    </el-form>
  </div>
      </div>
</template>


<script>
const axios=require("axios");

  export default {
    data() {
      var validateAccount = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('账号不能为空'));
        } else if(value.length <6 || value.length > 12) {
          callback(new Error('账号长度6--12'));
        } else {
          
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('密码不能为空'));
        } else if(value.length < 6 || value.length > 12) {
          callback(new Error('密码长度6--12'));
        } else {
         
          callback();
        }
      };
      return {
        ruleForm: {
          account: '',
          password: ''
        },
        rules: {
          account: [
            { validator: validateAccount, trigger: 'blur' },
          ],
          password: [
           { validator: validatePass, trigger: 'blur' },
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
           
          axios.post("http://localhost:8080/hpl-admin/login",{
                      account:this.ruleForm.account,
                      password:this.ruleForm.password
                    },{withCredentials:false}).then(r=>{
                        if(r.data.code===200){
                            this.$message({
                                message: '恭喜你，登陆成功',
                                type: 'success'
                            });
                            localStorage.setItem("jwt",r.headers.jwt);
                            location.href="/";
                        }else if(r.data.code===500){
                          //后台登录失败
                            this.$message.error(r.data.msg);
                        }else{
                          //数据库出错
                          this.$message.error("系统维护中！");
                        }
                      });
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>

</style>